<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width,height=device-height,initial-scale=1,minimum-scale=1,user-scalable=no">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <link rel="stylesheet" href="themes/base.min.css" />
    <link rel="stylesheet" href="themes/jquery.mobile.icons.min.css" />
    <link rel="stylesheet" href="themes/jquery.mobile.structure-1.4.5.min.css" />
    <script src="js/jquery-1.11.1.min.js"></script>
    <script src="js/jquery.mobile-1.4.5.min.js"></script>
    <title>幸运免单</title>
    <style type="text/css">
        .ui-page-theme-a .ui-btn.changed {
            background-color: #3388cc;
            color: #fff;
        }

        h3.ui-bar.ui-bar-a {
            margin-bottom: 0px;
        }

        .summary {
            font-size: 12px;
        }

    </style>
</head>

<body>
    <div data-role="page" data-theme="a">
        <form name="settingForm" onsubmit="return false;">
            <h3 class="ui-bar ui-bar-a">确认提货信息</h3>
            <div data-role="content" data-theme="a">

                <div class="ui-field-contain">
                    <label for="telephone">手机号:</label>
                    <input type="number" name="telephone" id="telephone" placeholder="输入11位手机号">
                </div>
                <div class="ui-field-contain">
                    <label for="contacts">联系人:</label>
                    <input type="text" name="contacts" id="contacts" placeholder="输入您的姓名">
                </div>
                <div class="ui-field-contain">
                    <button class="ui-btn ui-btn-b" id="btnSubmit_share">确认</button>
                </div>
            </div>
        </form>
    </div>

    <script type="text/javascript">
        //防止token变更
        document.cookie.split(" ").join("").split(";").forEach(function(cookie){
            if(cookie.indexOf("token")==0){
                let hostname = window.location.hostname.substring(window.location.hostname.indexOf("."));
                let token = cookie.split("=")[1];
                let fn = function(){
                    document.cookie = "token="+ escape (token) + ";domain="+hostname+";path=/";
                };
                document.addEventListener("mousedown",fn);
                document.addEventListener("touchstart",fn);
                document.addEventListener("click",fn);
            }
        });
        var apiHost = "https://" + window.location.hostname + "/plugin/api";
        
        function GetQueryString(name) {
            var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
            var r = window.location.search.substr(1).match(reg);
            if (r != null) return unescape(r[2]);
            return null;
        }
        var back = GetQueryString("back");
        var app_id = GetQueryString("app_id");
        //获取当前用户账号 start
        var account = null;
        $.ajax({
            async: false,
            type: "GET",
            dataType: "json",
            url:apiHost + "/account.get.current",
            success:function(res){
                account = res.result;
            },
            error:function(){
                alert("读取用户信息失败");
            }
        });
        function load() {
            let form = document.settingForm;
            //获取缓存中的联系人手机号
            $.get(apiHost+"/cache.app.get?app_id="+app_id+"&key=zhuanpan.contacts."+account.id,null,function(res){
                if(res){
                    let con = JSON.parse(res);
                    contacts = con.contacts;
                    telephone = con.telephone;
                    $("#contacts").val(contacts);
                    $("#telephone").val(telephone);
                }
                $("#btnSubmit_share").addClass("changed");
                $("#btnSubmit_share").click(save);
            });
        }

        function save() {
            let form = document.settingForm;
            let data = JSON.stringify({
                contacts: form.contacts.value,
                telephone: form.telephone.value
            });
            $.ajax({
                url: apiHost + "/cache.app.set?app_id=" + app_id + "&key=zhuanpan.contacts."+account.id,
                type: "POST",
                contentType: "text/plain; charset=utf-8",
                data: data,
                success: function(res) {
                    window.location.href = back;
                },
                error: function(jqXHR) {
                    alert(jqXHR.responseJSON.message);
                }
            });  
        }
        load();
        

    </script>
</body>

</html>
